package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@RequestMapping("/dev")
@Controller
public class HelloController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    //定义入参参数
    @RequestMapping("/hello")
    //required = false 表示参数 name 是可选的
    public ModelAndView hello(@RequestParam(name = "name11", required = false) String name) throws Exception {
//        LOGGER.info("进入控制器hello方法,请求路径为：{}", request.getRequestURI());
        LOGGER.info("你好{},欢迎学习SpringMVC", name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", name);
        modelAndView.setViewName("hello"); //设置跳转
        return modelAndView;
    }

    @RequestMapping("/welcome")
    public String welcome(HttpServletRequest request) throws Exception {
        LOGGER.info("进入控制器welcome方法,请求路径为：{}", request.getRequestURI());
        return "hello";
    }

    @RequestMapping("/model")
    public String model(Model model,
                        @RequestParam(name = "realName", required = false) String name) throws Exception {
        LOGGER.info("你好{},在参数注入Model对象也可以出参", name);
        model.addAttribute("realName", name);
        //创建SysUser对象 并存入model对象
        SysUser sysUser = new SysUser();
        sysUser.setRealName(name);
        model.addAttribute("sysUser", sysUser);
        return "hello";
    }

    @RequestMapping("/map")
    public String map(Map map, @RequestParam(name = "realName", required = false) String name){
        LOGGER.info("你好{},在参数注入Map对象也可以出参", name);
        map.put("realName", name);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(name);
        map.put("sysUser", sysUser);
        return "hello";
    }
}